/**
 * 
 */
package com.wsj.web.action.homeSiteNews;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.wsj.service.IBmClassTypeServ;
import com.wsj.service.IHomeSiteNewsClassServ;
import com.wsj.service.IHomeSiteNewsSearchServ;
import com.wsj.service.IHomeSiteNewsServ;
import com.wsj.service.IUserManagerServ;
import com.wsj.util.exception.BusinessException;
import com.wsj.util.page.PageResultSet;
import com.wsj.util.tool.NumberUtil;
import com.wsj.web.action.BaseAction;
import com.wsj.web.constant.homeSiteNews.HomeSiteNewsConstant;
import com.wsj.web.constant.homeSiteNews.IndexPageContent;
import com.wsj.web.constant.page.PageContent;
import com.wsj.web.subclass.displayBean.HomeSiteNews.HomeSiteIndexPageColumnBean;
import com.wsj.web.vo.homeSite.HomeSiteNewsClass;

/**
 * @author fengbin  createTime:2009-7-20
 *
 */
public class HomeSiteSearchAction extends BaseAction {
	
	/** 资讯分类接口 */
	private IHomeSiteNewsClassServ homeSiteNewsClassServ;
	
	/** 资讯接口 */
	private IHomeSiteNewsServ homeSiteNewsServ;
	
	/******
	 * 用户接口
	 */
	private IUserManagerServ userManagerServ;
	
	/*********
	 * 类型
	 */
	private IBmClassTypeServ bmClassTypeServ;
	
	/*********
	 * 资讯搜索接口
	 */
	private IHomeSiteNewsSearchServ homeSiteNewsSearchServ;
	
	
	
	/******
	 * 
	 * 站点资讯搜索
	 * 
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String classIdStr = request.getParameter("classId");
		String keyWords = request.getParameter("keyWords");
		String q = request.getParameter("q");
		
		if(q!=null  && !q.equals("")){
			keyWords = q;
		}else{
			if(keyWords==null || keyWords.equals("")){
				keyWords = "";
			}else{
				keyWords = new String(keyWords.getBytes("ISO-8859-1"),"UTF-8");
			}
		}
		
		
		
		request.setAttribute("q", keyWords);
		request.setAttribute("keyWords", keyWords);
		
		String pageStr = request.getParameter("page");
		String pageSizeStr = request.getParameter("pageSize"); 
		
		long classId =0 ;
		
		if(classIdStr!=null  &&  !classIdStr.equals("") && NumberUtil.isNumber(classIdStr)){
			classId =  Long.valueOf(classIdStr);
			request.setAttribute("classId", classId);
			
			//获取类目对象,获取导航属性
			List<HomeSiteNewsClass> navList = this.getNavListByClassId(classId); 
			request.setAttribute("navList", navList);
		}
			
			
			int page=HomeSiteNewsConstant.DEFAULT_PAGE;
			
			int pageSize = HomeSiteNewsConstant.DEFAULT_PAGE_SIZE;
			
			if(pageStr!=null  && !pageStr.equals("")){
				page = Integer.valueOf(pageStr);
			}
			
			if(pageSizeStr!=null  && !pageSizeStr.equals("")){
				pageSize = Integer.valueOf(pageSizeStr);
			}
			
		
			
			//获取左边推荐新闻
			this.getLeftPageCommentNews(request);
			
			
			//获取类目下的所有资讯列表
			PageResultSet  pageResultSet = this.homeSiteNewsSearchServ.loadHomeSiteNewsListByClassId(classId,keyWords,page,pageSize);
			
			if(pageResultSet!=null)
			{
			  	// 设置上一页
				request.setAttribute(PageContent.backPage,pageResultSet.getBackPage());
				//下一页
				request.setAttribute(PageContent.nextPage,pageResultSet.getNextPage());
				//当前页
				request.setAttribute(PageContent.curPage,pageResultSet.getCurPage());
				
				//总共页数
				request.setAttribute(PageContent.allPage,pageResultSet.getAllPage());
				//大小
				request.setAttribute(PageContent.pageSize,pageResultSet.getPageSize());
				
				int curDisplayPage = 1;
				if(pageResultSet.getCurPage()>0){
					
					if(pageResultSet.getCurPage()%10 ==0 ){
						curDisplayPage =(int) pageResultSet.getCurPage() /10;	
					}else{
						curDisplayPage =((int) pageResultSet.getCurPage() /10) +1;
					}
					
				}
				//分页数显示的当前页
				request.setAttribute("curDisplayPage",curDisplayPage);
				

				//结果集
				List result=null;
				if(pageResultSet!=null)
				{
					result =pageResultSet.getResult();
				}
				
				else
				{
					result=new ArrayList();
				}
				
				request.setAttribute("homeSiteNewsList", result);
				
			
			
			
		}
		
		return mapping.findForward("homeSiteNewsList");
		
		
	}

	


	/**********
	 * 获取左边推荐新闻
	 * @param request
	 */ 
	private void getLeftPageCommentNews(HttpServletRequest request) {
		
		//列表页 左边推荐新闻  10条文本新闻 0条图片新闻 0条引导阅读新闻
		HomeSiteIndexPageColumnBean COLUMN_SEARCH_COMMEND_NEWS_1 = this.homeSiteNewsServ.getColumnNews(IndexPageContent.SEARCH_COMMEND_NEWS_1_NAME,6,0,IndexPageContent.GUIDE_NEWS_DISPLAY_NO);
		request.setAttribute("COLUMN_SEARCH_COMMEND_NEWS_1", COLUMN_SEARCH_COMMEND_NEWS_1);
		
		//列表页 左边推荐新闻  10条文本新闻 0条图片新闻 0条引导阅读新闻
		HomeSiteIndexPageColumnBean COLUMN_SEARCH_COMMEND_NEWS_2 = this.homeSiteNewsServ.getColumnNews(IndexPageContent.SEARCH_COMMEND_NEWS_2_NAME,6,0,IndexPageContent.GUIDE_NEWS_DISPLAY_NO);
		request.setAttribute("COLUMN_SEARCH_COMMEND_NEWS_2", COLUMN_SEARCH_COMMEND_NEWS_2);
	}



	private List<HomeSiteNewsClass> getNavListByClassId(long classId) {
		
		List<HomeSiteNewsClass> homeSiteNewsClassList = null;
		try {
			
			HomeSiteNewsClass homeSiteNewsClass =this.getHomeSiteNewsClassServ().loadNewsClassById(classId);
			if(homeSiteNewsClass!=null){
				String seq=homeSiteNewsClass.getSeq();
				homeSiteNewsClassList = this.getHomeSiteNewsClassServ().getHomeSiteNewsClassListBySeq(seq);
			}
		} catch (BusinessException e) {
			this.getLogger().error("获取导航属性出错 error",e);
		}
		
		return homeSiteNewsClassList;
	}




	public IBmClassTypeServ getBmClassTypeServ() {
		return bmClassTypeServ;
	}



	public void setBmClassTypeServ(IBmClassTypeServ bmClassTypeServ) {
		this.bmClassTypeServ = bmClassTypeServ;
	}



	public IHomeSiteNewsClassServ getHomeSiteNewsClassServ() {
		return homeSiteNewsClassServ;
	}



	public void setHomeSiteNewsClassServ(
			IHomeSiteNewsClassServ homeSiteNewsClassServ) {
		this.homeSiteNewsClassServ = homeSiteNewsClassServ;
	}



	public IHomeSiteNewsSearchServ getHomeSiteNewsSearchServ() {
		return homeSiteNewsSearchServ;
	}



	public void setHomeSiteNewsSearchServ(
			IHomeSiteNewsSearchServ homeSiteNewsSearchServ) {
		this.homeSiteNewsSearchServ = homeSiteNewsSearchServ;
	}



	public IHomeSiteNewsServ getHomeSiteNewsServ() {
		return homeSiteNewsServ;
	}



	public void setHomeSiteNewsServ(IHomeSiteNewsServ homeSiteNewsServ) {
		this.homeSiteNewsServ = homeSiteNewsServ;
	}



	public IUserManagerServ getUserManagerServ() {
		return userManagerServ;
	}



	public void setUserManagerServ(IUserManagerServ userManagerServ) {
		this.userManagerServ = userManagerServ;
	}



	
	
		

}
